<template>
  <div class="mian-container">
    <div class="operation">
      <el-button type="primary" plain size="small" @click="handleAdd"
        >新增</el-button
      >
      <el-button type="danger" plain size="small" @click="delBatch"
        >批量删除</el-button
      >
    </div>

    <div class="table">
      <el-table
        :data="tableData"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column prop="imgUrl" label="广告图片" show-overflow-tooltip>
          <template v-slot="scope">
            <el-image
              style="width: 100px; height: 50px"
              :src="scope.row.imgUrl"
              fit="fill"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="is_hot" label="广告上下架" show-overflow-tooltip>
          <template v-slot="scope">
            <el-button
              v-if="!scope.row.is_hot"
              plain
              type="primary"
              size="mini"
              @click="putOnAndDown(scope.row)"
              >上架</el-button
            >
            <el-button
              v-else
              plain
              type="info"
              size="mini"
              @click="putOnAndDown(scope.row)"
              >下架</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="time" label="创建时间"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template v-slot="scope">
            <el-button
              plain
              type="primary"
              size="mini"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              plain
              type="danger"
              size="mini"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          :total="total"
          layout="total,prev,pager,next"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      title="广告管理"
      :visible.sync="showVisiable"
      width="40%"
      :close-on-click-modal="false"
      destory-on-close
    >
      <el-form label-width="100px" :model="form">
        <el-form-item label="图片上传" prop="imgUrl">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:3000/files/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="this.form.imgUrl"
              :src="this.form.imgUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqAdInfo,
  addAdvertisment,
  updateAd,
  delAd,
  delBatchAd,
  handleAd
} from "@/api/ad.js";
export default {
  name: "ad",
  data() {
    return {
      tableData: [{ imgUrl: "123", is_hot: false, time: "123" }],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      showVisiable: false,
      form: {},
      delBatchData: [],
    };
  },
  methods: {
    async save() {
      if (!this.form._id) {
        let result = await addAdvertisment(this.form);
        if (result.code == 200) {
          this.$message.success(result.msg);
          this.showVisiable = false;
          this.pageNo = 1;
          this.getAdInfo(this.pageNo, this.pageSize);
        } else {
          this.$message.error(result.msg);
        }
      } else {
        let result = await updateAd(this.form);
        if (result.code == 200) {
          this.$message.success(result.msg);
          this.showVisiable = false;
          this.pageNo = 1;
          this.getAdInfo(this.pageNo, this.pageSize);
        } else {
          this.$message.error(result.msg);
        }
      }
    },

    cancle(){
      this.showVisiable = false
    },

    handleSelectionChange(val) {
      let arr = val.map((item) => {
        return item._id;
      });
      this.delBatchData = arr;
    },

    handleCurrentChange(val) {
      this.pageNo = val;
      this.getAdInfo(this.pageNo, this.pageSize);
    },

    handleSizeChange(val) {
      this.pageNo = val;
      this.getAdInfo(this.pageNo, this.pageSize);
    },

    handleAdd() {
      this.form = {};
      this.showVisiable = true;
    },

    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.showVisiable = true;
    },

    async handleDelete(row) {
      let data = { _id: row._id };
      let result = await delAd(data);
      if (result.code == 200) {
        this.$message.success(result.msg);
        this.pageNo = 1;
        this.getAdInfo(this.pageNo, this.pageSize);
      } else {
        this.$message.error(result.msg);
      }
    },

    async delBatch() {
      let result = await delBatchAd(this.delBatchData);
      if (result.code == 200) {
        this.$message.success(result.msg);
        this.pageNo = 1;
        this.getAdInfo(this.pageNo, this.pageSize);
      } else {
        this.$message.error(result.msg);
      }
    },

    async getAdInfo(pageNo, pageSize) {
      let result = await reqAdInfo(pageNo, pageSize);
      if (result.code == 200) {
        this.tableData = result.data;
        this.total = result.total;
      } else {
        this.$message.error(result.msg);
      }
    },

    // 广告上下架
    async putOnAndDown(row) {
      let data = {_id:row._id,is_hot:!row.is_hot}
      let result = await handleAd(data)
      if(result.code == 200){
        this.$message.success(result.msg)
        this.pageNo = 1
        this.getAdInfo(this.pageNo,this.pageSize)
      }else {
        this.$message.error(result.msg)
      }
    },

    // 图片上传成功的方法
    handleAvatarSuccess(res, file) {
      this.$set(this.form, "imgUrl", res.url);
    },

    // 图片上传前判断格式
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 5MB!");
      }
      return isJPG && isLt2M;
    },
  },
  mounted() {
    this.getAdInfo(this.pageNo, this.pageSize);
  },
};
</script>

<style lang="scss" scoped>
.main-container {
  .operation {
    margin-top: 5px;
    padding: 10px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.1);
  }
  .table {
    margin-top: 5px;
    padding: 15px 10px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
